package web.controller;
import java.net.MalformedURLException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.json.*;
import service.treemanager.ITreeManager;
import web.form.SheetInfo;
import web.form.SheetType;

/**
 * <b>TreeviewController  </b>
 * <p>TreeviewController controle les requêtes pour renvoyer l'arborescence en utilisant le service de gestion de fiches.</p>
 * 
 */
@Controller
public class TreeviewController {

	/**
     * Repr&eacute;sente le service de gestion de fiches
     */
	@Resource
	private ITreeManager treeManager;
	
	/**
     * Controlleur qui renvoi la page de l'arborescence
     */
	@RequestMapping(value = "/treeview.htm", method = RequestMethod.GET)
	public String handleRequestTreeview(HttpSession session, ModelMap model,
			@ModelAttribute("message") String message){
		List <SheetType> typeList = treeManager.getRootTypeViews();
		model.addAttribute("list",typeList);
		model.addAttribute("rootname", treeManager.getRootName());
		
		if(message!=null) {
			model.addAttribute("message", message);
		}
		
		if(treeManager.isGeneratingTree()) {
			model.addAttribute("generating", true);
		}
		
		return "treeView";
	}
	
	/**
     * Controlleur qui recoi une requête pour renvoyer un élément de l'arborecence sous format JSON
     */
	@RequestMapping(value = "/json.htm", method = RequestMethod.POST)
	public String handleRequestJson(HttpSession session, ModelMap model,@RequestParam(value="root", required=false) String root) throws JSONException, InterruptedException, MalformedURLException  {

		JSONArray jsonArray = new JSONArray();
		String[] rootsplit = root.split("-");
		
		if(rootsplit.length >= 2) {
			List <SheetInfo> sheetList = null;
			if (rootsplit[1].equals(treeManager.getRootName())) {
				sheetList = treeManager.getRootsByType(rootsplit[0]);
			} else {
				sheetList = treeManager.getChildren(rootsplit[1], rootsplit[0]);
			}
			for(SheetInfo sheet : sheetList){
				JSONObject json = new JSONObject();
				json.put("text", sheet.getCode() + " : " + sheet.getTitle());
				if(treeManager.getChildrenTypes(sheet.getCode()).isEmpty())
					json.put("hasChildren", false);
				else
					json.put("hasChildren", true);
				
				//créer url pour la fiche
				URIParser uri = new URIParser("sheet.htm");
				uri.setParameter("code",sheet.getCode());
				uri.setParameter("type",sheet.getType());
				uri.removeParameter("register");
				uri.removeParameter("login");
				
				json.put("id", sheet.getCode());
				json.put("href", uri.getUrl());
				jsonArray.put(json);
			}
		} else {
			for(SheetType sheet : treeManager.getChildrenTypes(root)){
				JSONObject json = new JSONObject();
				json.put("text", sheet.getLabel());
				json.put("hasChildren", true);
				json.put("id", sheet.getName()+"-"+root);
				jsonArray.put(json);
			}
		}
		
		model.addAttribute("jsonArray", jsonArray);
		return "json";
	}	
	
}
